package pl.freetimetool.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class WorkingDaysResolver {
	
	public static int workingDaysBetween (Date startDate, Date endDate){
		
		
		
		
		int daysBetween = 1; //because always we add 1 day
		GregorianCalendar startCal = new GregorianCalendar(new Locale("PL"));
		GregorianCalendar endCal = new GregorianCalendar(new Locale("PL"));
		startCal.setTime(startDate);
		endCal.setTime(endDate);
		
		if(endCal.before(startCal)){
			throw new IllegalArgumentException("end date can't be before start date.");
		}
		
		while (startCal.before(endCal)) {
			int dayOfWeek = startCal.get(Calendar.DAY_OF_WEEK);
			
			if (dayOfWeek != Calendar.SATURDAY && dayOfWeek != Calendar.SUNDAY) {
				daysBetween++;
			}
			startCal.add(Calendar.DAY_OF_MONTH, 1);
		}
		
		return daysBetween;
	}

}
